//
//  AppDelegate.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/14.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        
        QorumLogs.enabled = true
        // 设置导航栏颜色
        setNavTiner()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultViewController
        
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwiftNotificationKey, object: nil, queue: nil) { (Notification) -> Void in
            
            if Notification.object != nil {
                
                self.window?.rootViewController = WelcomeViewController()
                
            } else{
                
                self.window?.rootViewController = MainViewController()
            }
        }
        
        return true
    }
    
    ///  设置导航栏颜色
    private func setNavTiner(){
        UINavigationBar.appearance().tintColor = weiboViewTinerColor
        UITabBar.appearance().tintColor = weiboViewTinerColor
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        
        StatusDAL.clearMermory()
        
    }

}
// MARK: - 判断是否登录,选择界面
extension AppDelegate {
    
    var defaultViewController:UIViewController{
        
        return OauthViewModel.sharedOauthViewModel.isLogin ? currentVersionViewController : MainViewController()
        
    }
    
}


// MARK: - 判断是否时最新版本,选择显示界, 新版本显示新特性界面,不是新版本显示欢迎界面
extension AppDelegate {
    
    /// 判断是否是新版本,选择界面(新特性:欢迎界面)
    var currentVersionViewController:UIViewController{
        
        return isNewVersion ? NewfeatureViewController() : WelcomeViewController()
    
    }
    
    /// 判断是不是新版本
    var isNewVersion:Bool{
        
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String) ?? 0.0
        
        let preVersionKey = "preVersionKey"
        
        let preVersion = NSUserDefaults.standardUserDefaults().doubleForKey(preVersionKey)

        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: preVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        return preVersion < currentVersion
    }
    
}